package com.baomidou.springwind.controller;

import com.baomidou.framework.controller.SuperController;
import com.baomidou.kisso.common.util.RandomUtil;
import com.baomidou.springwind.common.MyCaptcha;
import com.baomidou.springwind.service.IPermissionService;
import com.baomidou.springwind.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.kisso.annotation.Action;
import com.baomidou.kisso.annotation.Login;
import com.baomidou.kisso.annotation.Permission;

/**
 * <p>
 * 首页
 * </p>
 *
 * @author hubin
 * @Date 2016-04-13
 */
@Controller
public class IndexController extends SuperController {

    @Autowired
    protected IUserService userService;

    @Autowired
    private IPermissionService permissionService;


    /**
     * 首页
     */
    @Login(action = Action.Skip)
    @Permission(action = Action.Skip)
    @RequestMapping("/")
    public String login(Model model) {
        model.addAttribute(MyCaptcha.CAPTCHA_TOKEN, RandomUtil.get32UUID());
        return "/login1";
    }

    /**
     * 登录
     */
    @RequestMapping("/index")
    @Permission("1001")
    public String index(Model model) {
        return pjaxPath("/common/home", model);
    }

    /**
     * 主页
     */
    @Permission(action = Action.Skip)
    @RequestMapping("/inConstruction")
    public String inConstruction(Model model) {
        return pjaxPath("/error/inConstruction", model);
    }

    protected String pjaxPath(String url, Model model) {
        String header = request.getHeader("X-PJAX");
        if (header == null || !header.equals("true")) {
            model.addAttribute("currentUser", userService.selectById(getCurrentUserId()));
            model.addAttribute("menuList", permissionService.selectMenuVOByUserId(getCurrentUserId()));
            model.addAttribute("mainUrl", url);
            return "/index1";
        }
        return url;
    }
}
